﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLVatTu.Class;
using QLVatTu.InBaoCao;

namespace QLVatTu.BaoCao
{
    public partial class frmBaoCaoChuyenKho : Form
    {
        KetNoiDB cn = new KetNoiDB();

        public frmBaoCaoChuyenKho()
        {
            InitializeComponent();
        }

        private void loadData()
        {
            cmbKhoChuyen.DataSource = cn.getTable("SELECT [MaKho],[TenKho] FROM [tbl_Kho]");
            cmbKhoChuyen.DisplayMember = "TenKho";
            cmbKhoChuyen.ValueMember = "MaKho";

            load_grid_view();
        }

        private void load_grid_view(string dieuKien = null)
        {
            string sql = "";
            if (dieuKien != null)
            {
                sql = @"
                    SELECT    ROW_NUMBER() over(order by dbo.tbl_ChuyenKho.MaChuyenKho) as STT,dbo.tbl_ChuyenKho.MaChuyenKho as PhieuChuyen, dbo.tbl_ChuyenKho.NgayChuyen as NgayLap, SUM(dbo.tbl_ChiTietChuyenKho.SoLuong) as SoLuong
                    FROM         dbo.tbl_ChuyenKho INNER JOIN
                                          dbo.tbl_ChiTietChuyenKho ON dbo.tbl_ChuyenKho.MaChuyenKho = dbo.tbl_ChiTietChuyenKho.MaChuyenKho
                    "
                    + dieuKien + @"
                    GROUP BY dbo.tbl_ChuyenKho.MaChuyenKho, dbo.tbl_ChuyenKho.NgayChuyen
                ";
            }
            else
            {
                sql = @"
                    SELECT    ROW_NUMBER() over(order by dbo.tbl_ChuyenKho.MaChuyenKho) as STT,dbo.tbl_ChuyenKho.MaChuyenKho as PhieuChuyen, dbo.tbl_ChuyenKho.NgayChuyen as NgayLap, SUM(dbo.tbl_ChiTietChuyenKho.SoLuong) as SoLuong
                    FROM         dbo.tbl_ChuyenKho INNER JOIN
                                          dbo.tbl_ChiTietChuyenKho ON dbo.tbl_ChuyenKho.MaChuyenKho = dbo.tbl_ChiTietChuyenKho.MaChuyenKho
                    WHERE tbl_ChuyenKho.XacNhan = 1
                    GROUP BY dbo.tbl_ChuyenKho.MaChuyenKho, dbo.tbl_ChuyenKho.NgayChuyen
                ";
            }
            dgvPhieuChuyen.DataSource = cn.getTable(sql);
        }

        private void frmBaoCaoChuyenKho_Load(object sender, EventArgs e)
        {
            if (cn.KetNoi())
            {
                loadData();
            }
            else
            {
                MessageBox.Show("Kết nối CSDL thất bại!");
                this.Close();
            }
        }

        private string XacDinhDieuKienLoc()
        {
            string dieuKienLoc = "WHERE tbl_ChuyenKho.XacNhan = 1 ";
            if (ckbKhoChuyen.Checked == true && cmbKhoChuyen.Items.Count > 0)
            {
                dieuKienLoc += " AND dbo.tbl_ChuyenKho.TuKho = '" + cmbKhoChuyen.SelectedValue.ToString() + "'";
            }

            // Dieu kien loc ngay thang
            if (rbtnLocThang.Checked == true && cmbThang.Text != "")
            {
                dieuKienLoc += " AND MONTH(dbo.tbl_ChuyenKho.NgayChuyen) = " + cmbThang.Text + " AND YEAR(dbo.tbl_ChuyenKho.NgayChuyen) = " + nudNamThang.Value.ToString();
            }
            else if (rbtnLocQuy.Checked == true)
            {
                if (cmbQuy.Text == "1")
                {
                    dieuKienLoc += " AND dbo.tbl_ChuyenKho.NgayChuyen BETWEEN '" + nudNamQuy.Value.ToString() + "-01-01" + "' AND '" + nudNamQuy.Value.ToString() + "-03-31'";
                }
                else if (cmbQuy.Text == "2")
                {
                    dieuKienLoc += " AND dbo.tbl_ChuyenKho.NgayChuyen BETWEEN '" + nudNamQuy.Value.ToString() + "-04-01" + "' AND '" + nudNamQuy.Value.ToString() + "-06-30'";
                }
                else if (cmbQuy.Text == "3")
                {
                    dieuKienLoc += " AND dbo.tbl_ChuyenKho.NgayChuyen BETWEEN '" + nudNamQuy.Value.ToString() + "-07-01" + "' AND '" + nudNamQuy.Value.ToString() + "-09-30'";
                }
                else if (cmbQuy.Text == "4")
                {
                    dieuKienLoc += " AND dbo.tbl_ChuyenKho.NgayChuyen BETWEEN '" + nudNamQuy.Value.ToString() + "-10-01" + "' AND '" + nudNamQuy.Value.ToString() + "-12-31'";
                }
            }
            else if (rbtnLocNgay.Checked == true)
            {
                dieuKienLoc += " AND dbo.tbl_ChuyenKho.NgayChuyen BETWEEN '" + dtipTuNgay.Value.ToString("yyyy-MM-dd") + "' AND '" + dtipDenNgay.Value.ToString("yyyy-MM-dd") + "'";
            }
            return dieuKienLoc;
        }

        private void btnLoc_Click(object sender, EventArgs e)
        {
            load_grid_view(XacDinhDieuKienLoc());
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            frmInBaoCaoChuyenKho frm = new frmInBaoCaoChuyenKho();
            string dieuKien = XacDinhDieuKienLoc();
            string sql = @"
                    SELECT    ROW_NUMBER() over(order by dbo.tbl_ChuyenKho.MaChuyenKho) as STT,dbo.tbl_ChuyenKho.MaChuyenKho as PhieuChuyen, dbo.tbl_ChuyenKho.NgayChuyen as NgayLap, SUM(dbo.tbl_ChiTietChuyenKho.SoLuong) as SoLuong
                    FROM         dbo.tbl_ChuyenKho INNER JOIN
                                          dbo.tbl_ChiTietChuyenKho ON dbo.tbl_ChuyenKho.MaChuyenKho = dbo.tbl_ChiTietChuyenKho.MaChuyenKho
                    "
                    + dieuKien + @"
                    GROUP BY dbo.tbl_ChuyenKho.MaChuyenKho, dbo.tbl_ChuyenKho.NgayChuyen
                ";
            frm.BaoCaoChuyenKhoBindingSource.DataSource = cn.getTable(sql);
            frm.ShowDialog();
        }

        private void ckbKhoChuyen_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbKhoChuyen.Checked == true)
            {
                cmbKhoChuyen.Enabled = true;
            }
            else
            {
                cmbKhoChuyen.Enabled = false;
            }
        }

        private void rbtnLocThang_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnLocThang.Checked == true)
            {
                cmbThang.Enabled = true;
                nudNamThang.Enabled = true;
            }
            else
            {
                cmbThang.Enabled = false;
                nudNamThang.Enabled = false;
            }
        }

        private void rbtnLocQuy_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnLocQuy.Checked == true)
            {
                cmbQuy.Enabled = true;
                nudNamQuy.Enabled = true;
            }
            else
            {
                cmbQuy.Enabled = false;
                nudNamQuy.Enabled = false;
            }
        }

        private void rbtnLocNgay_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnLocNgay.Checked == true)
            {
                dtipTuNgay.Enabled = true;
                dtipDenNgay.Enabled = true;
            }
            else
            {
                dtipTuNgay.Enabled = false;
                dtipDenNgay.Enabled = false;
            }
        }
    }
}
